$(function () {
    var dataTypes = '',
        urlTypeImg = '/home/getHomeBannerInfos',//banner 图片接口
        getUrl = '/home/getHomeRecommendBeefInfos',    // 页面list数据接口地址
        meatTypes = 0,
        categoryApi = '/category/getMeatCategoryInfos',
        jrollinit = null,
        container = $('#meal-info section'),
        setmeatType = function (obj) {
            obj.meatTypes = obj.meatTypes ? obj.meatTypes : 0;
            obj.pageSize = obj.pageSize ? obj.pageSize : 10;
            obj.offset = obj.offset ? obj.offset : 0;
            obj.time = obj.time ? obj.time : '';
            obj.keyWord = obj.keyWord ? obj.keyWord : '';
            obj.country = obj.country ? obj.country : '';
            dataTypes = "meatType=" + obj.meatTypes + "&pageSize=" + obj.pageSize + "&offset=" + obj.offset + "&updateDate=" + obj.time + "&keyWord=" + obj.keyWord + "&country=" + obj.country;
            return dataTypes;
        },
        requestData = function (obj,fun) {
            $.ajax({
                url: host + getUrl,
                type: 'post',
                data: obj,
                success: function (data) {
                   fun(data)
                }
            });
        },
        filePage = function () {
        var indexData = JSON.parse(window.sessionStorage.getItem('indexData')),
            meatType = indexData.meatTypes;
            meatTypes = meatType;
        window.sessionStorage.removeItem('index')
        window.sessionStorage.removeItem('indexData')

        $('.mety-nav li').each(function () {
            $(this).removeClass()
            if($(this).index()===meatType) $(this).addClass('font_color radius' +(+meatType+1) );
        })
        jrollinit.scroller.innerHTML = indexData.data;
        jrollinit.options.page = 1;
        jrollinit.refresh();
    };
    // 搜索页面
    $('#search input').on('focus', function () {
        searchPublish()
    })
    // 轮播图

    var mySwiper = new Swiper('.content1_1_left .swiper-container', {
        // direction: 'vertical',
        loop: true,
        autoplay: 1000,
        speed: 3000,
        loopAdditionalSlides: 1,
        autoplayDisableOnInteraction: false,
        pagination: '.swiper-pagination',
    })
// 首页导航
    $('#middle .row').children().eq(0).on('click', function () {
        window.location.href = 'tradingInstructions.html'
    })

    $('#middle .row').children().eq(1).on('click', function () {
        DetLogin(function () {
            window.location.href = 'iWhant.html';
            // ImporInfor()
            // $.ajax({
            //     url: host + '/myhome/shop/getMyAuthenticateStatus',
            //     data:Infornation,
            //     success:function (data) {
            //         console.log(data);
            //     }
            // })
        });
    })

    $('#middle .row').children().eq(2).on('click', function () {
        window.location.href = 'orderBuy.html'
    })
    // teb 蓝 切换
    $('.mety-nav li').on('click', function () {     //切换 tab 栏
        console.log(this.innerText)
        if($(this).hasClass('font_color')) {
            return;
        }
        // breakS = true
        // 给导航添加样式
        $('.mety-nav li').removeClass('font_color')
        $(this).addClass('font_color')
        if (this.innerText == ' 羊肉') {
            $(this).addClass('radius2')
            getUrl = '/home/getHomeRecommendMuttonInfos';
            meatTypes = 1;
        } else {
            $(this).addClass('radius1')
            getUrl = '/home/getHomeRecommendBeefInfos';
            meatTypes = 0;
        }
        jrollinit.destroy();
        var domNav = $('#meal-info .mety-nav').clone(true)
        $('#meal-info').empty()
        $('#meal-info').append($('<div class="container" id="scroller"></div>'));
        $('#meal-info').append($(domNav));
        jroll();
        jrollinit.refresh();
    });
    // 默认首屏渲染
    jroll();
    window.fn = function (Ele) {
        var obj = {
            data:jrollinit.scroller.innerHTML,
            meatTypes:meatTypes
        };
        window.sessionStorage.setItem('indexData', JSON.stringify(obj))
    }
    function jroll() {
        var breakR = true, firstEnd=true;
        jrollinit = new JRoll("#meal-info", {
            scrollBarY: true,
            scrollBarFade: true
        });   // jroll 对象

        // // 下拉刷新
        jrollinit.pulldown({
            spinning:false,
            refresh: function(complete) {
                jrollinit.options.page = 1;
                requestData(setmeatType({
                    meatTypes: meatTypes,
                    offset: jrollinit.options.page - 1
                }),function (data) {
                    if (data.success == true) {
                        complete();
                        jrollinit.scroller.innerHTML = "";    //清空内容
                        jrollinit.infinite_callback(data.data);
                    } else {
                        //无数据则返回什么
                        console.log(data)
                    }
                });
            },
            topIndex:'26'
        });
        // 无限加载
        jrollinit.infinite({
            total:1,
            options: jrollinit,
            completeTip: '<div class=\"jroll-infinite-tip\" style=\"text-align\: center\;\"><div class="tip"><p>对我们的供货推荐不满意？</p><p><span class="blue-color">点击供货</span>或<span class="blue-color">向下滑动</span>,查看更多供货消息吧</p></div></div>',
            loadingTip: '<div class=\"jroll-infinite-tip\" style=\"text-align\: center\;\">正在加载。。。</div>',
            // template: '<li onclick="fn(this)" ><a  href="deliveryDetails.html?history=true&id={{=_obj.id}}"><div class="row"><div class="col-xs-4" id="photo"><img src={{=_obj.imageList[0]?_obj.imageList[0]:"img\/defaultImg.png"}}></div><div class="col-xs-8 "><p>{{=_obj.meatCategory[0]?_obj.meatCategory[0]:"暂无数据"}}</p><p id="price"><span><em>¥{{=_obj.price?_obj.price+ "/吨":"议定"}}</em></span></p><p><span>产地：<em>{{=_obj.country}}</em></span><span id="margin">所在地：{{=_obj.warehouse}}</em></span></p><p><span>数量：<em>{{=Math.round(_obj.amount / 1000 * 100) / 100}}吨</em></span>  <span>供货类型：<em>{{=_obj.provideWay}}</em></p></div></div></a></li>',
            // <li onclick="fn(this)"><a href="deliveryDetails.html?history=true&id={{=_obj.id}}"><div class="row"><div class="photo"><img src={{=_obj.imageList[0]?_obj.imageList[0]:"img\/defaultImg.png"}}></div><div class="text"><div class="price"><span>￥</span>{{=_obj.price?_obj.price:"议定"}}<span>/吨</span> </div><p class="title">{{=_obj.meatCategory[0]?_obj.meatCategory[0]:"暂无数据"}}</p><p class="time">2015-12-17</p><p><span class="fl">数&nbsp;&nbsp;&nbsp;&nbsp;量：<span class="blue-color">{{=Math.round(_obj.amount / 1000 * 100) / 100}} </span>吨</span><span class="fr">包装：单独包装</span></p><p><span class="fl">所在地：<span>{{=_obj.warehouse}} </span></span><span class="fr">产地：{{=_obj.country}}</span></p></div></div></a> </li>
            template: '<li data-id={{=_obj.id}} data-xian={{=_obj.provideWay=="期货"?0:1}} onclick="fn(this)"><a href="deliveryDetails.html?index=true&id={{=_obj.id}}"><div class="row"><div class="photo"><img src={{=_obj.imageList[0]?_obj.imageList[0]:"img\/defaultImg.png"}}></div><div class="text"><div class="price"><span>￥</span>{{=_obj.price?_obj.price:"议定"}}<span>/吨</span></div><p class="title">{{=_obj.meatCategory[0]?_obj.meatCategory[0]:"暂无数据"}}</p><p class="time">{{=time(_obj.updateDate)}}</p><p><span class="fl">数<span style="visibility: hidden;">量</span>量：<span class="blue-color">{{=Math.round(_obj.amount / 1000 * 100) / 100}} </span>吨</span><span class="fr">厂号品牌：{{=_obj.factoryNo}}</span></p><p><span class="fl">所在地：<span>{{=_obj.warehouse}} </span></span><span class="fr">产品产地：{{=_obj.country}}</span></p><div class="xian {{=_obj.provideWay==\"期货\"?\"huang\":\"\"}}">{{=_obj.provideWay=="期货"?"期":"现"}}</div></div></div></a> </li>',
            getData: function (page, callback) {
                var history = window.sessionStorage.getItem('index'),indexData;
                    if(history){
                        filePage();
                    }else {
                        requestData(setmeatType({
                            meatTypes: meatTypes,
                            offset:0
                        }),function (data) {
                            if (data.success == true) {
                                callback(data.data)
                            } else {
                                //无数据则返回什么
                                console.log(data)
                            }
                        });
                    }
            },
        });
        jrollinit.on("scroll", function(e) {
            if(parseInt($('#meal-info').css('top')) <= 0 &&jrollinit.directionY===-1){
                $('#meal-info').css('top','0');
                return;
            }
            var num = $(this.scroller).css('transform').replace(/matrix\(|\)/g, '').split(', ')[5]
            num = Number(num);
            if(num < 0 && num >= -240){
            //     $('#meal-info').css('top',310+num)
                var topGo = 240 +num;
                $('#meal-info').css('top',topGo)
            }else if(num < -240 && breakR){
                breakR = false
                $('#meal-info').css('top',0)
                jrollinit.refresh();
            }else if(num >= 0 && !breakR){
                breakR = true;
                $('#meal-info').css('top',240)
                jrollinit.refresh();
            }
        });
        jrollinit.on("scrollEnd", function() {
            if (this.y === this.maxScrollY) {
                if(!firstEnd){
                    window.location.href = 'supply.html?meatType=' + meatTypes;
                }else {
                    firstEnd = false;
                }
            }
        });
        jrollinit.refresh();
        return jrollinit;
    };
    // 屏幕滑动事件
//realName实名认证

//尾部的点击事件

    $('#footer-nav  div').eq(5).on('click', function () {
        DetLogin(fnPerson);
    })
    function fnPerson() {
        window.location.href = 'personal.html';
    }


})
